<?php
$themename = "Viet Nam Wordpress Themes";
$shortname = THEME_NAME;
//Theme Options
$theme_options = array (
array(
      "name" => __("Cấu hình " . $themename, 'theme'),
      	"type" => "title"
      ),
      array(
      	"type" => "open"
      ),
      array(
      	'type'	=>	'sep',
      	'name'	=>	'Pages'	
      ),
      array(
	      'name'  =>  __('About us', 'theme'),
	      'desc'  =>  __('About us page', 'theme'),
	      'id'    =>  $shortname . '_about_page',
	      'type'  => 'pages',
      ),
      array(
      	'type'	=>	'sep',
      	'name'	=>	'Categories'	
      ),
      array(
	      'name'  =>  __('Feature Slider', 'theme'),
	      'desc'  =>  __('Feature Slider Caterory', 'theme'),
	      'id'    =>  $shortname . '_feature_slider_cat',
	      'type'  => 'categories',
      ),
      array(
	      'name'  =>  __('Số item hiển thị', 'theme'),
	      'desc'  =>  __('', 'theme'),
	      'id'    =>  $shortname . '_feature_slider_showposts',
	      'type'  => 'text',
      ),
      array(
	      'name'  =>  __('Portfolio', 'theme'),
	      'desc'  =>  __('Portfolio category', 'theme'),
	      'id'    =>  $shortname . '_portfolio_cat',
	      'type'  => 'categories',
      ),
      array(
	      'name'  =>  __('Số item hiển thị', 'theme'),
	      'desc'  =>  __('/page', 'theme'),
	      'id'    =>  $shortname . '_portfolio_showposts',
	      'type'  => 'text',
      ),
      array(
	      'name'  =>  __('Blog', 'theme'),
	      'desc'  =>  __('Blog category', 'theme'),
	      'id'    =>  $shortname . '_blog_cat',
	      'type'  => 'categories',
      ),
      array(
	      'name'  =>  __('Số item hiển thị', 'theme'),
	      'desc'  =>  __('/page', 'theme'),
	      'id'    =>  $shortname . '_blog_showposts',
	      'type'  => 'text',
      ),
      /*
      array(
      	'type'	=>	'sep',
      	'name'	=>	'Thumbnail Size'	
      ),
      array(
	      'name'  =>  __('Width', 'theme'),
	      'desc'  =>  __('Chiều rộng hình ảnh - thumbnail', 'theme'),
	      'id'    =>  $shortname . '_thumbnail_width',
	      'type'  => 'text',
      ),
      array(
	      'name'  =>  __('Height', 'theme'),
	      'desc'  =>  __('Chiều rộng hình ảnh - thumbnail', 'theme'),
	      'id'    =>  $shortname . '_thumbnail_height',
	      'type'  => 'text',
      ),
      /*array(
      	'type'	=>	'sep',
      	'name'	=>	'Trang hiển thị'	
      ),
      array(
	      'name'  =>  __('Trang tài trợ', 'theme'),
	      'desc'  =>  __('Chọn trang hiển thị nhà tài trợ', 'theme'),
	      'id'    =>  $shortname . '_sponsor_page',
	      'type'  => 'pages',
      ),
       array(
       'name'  =>  __('Show Links?', 'theme'),
       //'desc'  =>  __('Check to show "What\'s new"', 'theme'),
       'id'    =>  $shortname . '_is_show_links',
       'type'  => 'checkbox',
       ),*/
      array(  "type" => "close")
      );

      function theme_add_admin() {
      	global $themename, $shortname, $theme_options;
      	if ( $_GET['page'] == THEME_NAME . '-' . basename(__FILE__) ) {
      		if ( 'save' == $_REQUEST['action'] ) {
      			foreach ($theme_options as $value) {
          			update_option( $value['id'], $_REQUEST[ $value['id'] ] );
      			}
      			foreach ($theme_options as $value) {
		          if( isset( $_REQUEST[ $value['id'] ] ) ) {
		          	update_option( $value['id'], $_REQUEST[ $value['id'] ]  );
		          } else {
		          	delete_option( $value['id'] );
		          }
      			}
      			wp_redirect("themes.php?page=". THEME_NAME . '-' . basename(__FILE__) ."&saved=true");
      			die;
      		}else if( 'reset' == $_REQUEST['action'] ) {
      			foreach ($options as $value) {
          			delete_option( $value['id'] );
      			}
      			wp_redirect("themes.php?page=". THEME_NAME . '-' . basename(__FILE__) ."&reset=true");
      			die;
      		}
      	}
      	add_theme_page("Cấu hình " . $themename, "Theme Config", 'edit_themes', THEME_NAME . '-' . basename(__FILE__), 'vnwp_theme_admin');
      }

      function vnwp_theme_admin() {
      	global $themename, $shortname, $theme_options;
      	if ( $_REQUEST['saved'] )
      		echo '<div id="message" class="updated fade"><p><strong>'. __('Cài đặt đã được lưu.') .'</strong></p></div>';
      	if ( $_REQUEST['reset'] )
      		echo '<div id="message" class="updated fade"><p><strong>'. __('Cài đặt đã được khôi phục.') .'</strong></p></div>';
      	?>
	<div class="wrap">
		<style type="text/css">
		#theme-options td.td-left {
			text-align: right;
			padding-right: 10px;
		}
		
		#theme-options td.td-right input[type=text],select {
			width: 240px;
		}
		</style>
		<form method="post" action="">
		<?php foreach ($theme_options as $value) {
			switch ( $value['type'] ) {
			case "open":
				?>
				<table id="theme-options" width="49%" border="0"
					style="background-color: #eef5fb; padding: 10px;">
				<?php break;
			case "close":
				?>
				</table>
				<br />
				<?php break;
			case "sep":
				?>
				<tr>
					<th>
					<?php echo $value['name'];?>
					</th>
				</tr>
				<?php 
				break;	
			case "title":
				?>
				<table width="100%" border="0"
					style="background-color: #0d73b4; padding: 0 10px; -moz-border-radius: 5px 5px 0 0;">
					<tr>
						<td colspan="1">
							<h1 style="font-family: Arial; color: #fff; font-size: 15px;">
							<?php echo $value['name']; ?>
							</h1>
						</td>
					</tr>
				</table>
				<?php break;
			case 'text':
				?>
				<tr>
					<td width="25%" class="td-left"><?php echo $value['name']; ?></td>
					<td class="td-right"><input name="<?php echo $value['id']; ?>"
						id="<?php echo $value['id']; ?>"
						type="<?php echo $value['type']; ?>"
						value="<?php if ( get_option( $value['id'] ) != "") { echo get_option( $value['id'] ); } else { echo $value['std']; } ?>" />
						<small><?php echo $value['desc']; ?> </small>
					</td>
				</tr>
				<?php break;
			case 'textarea':
				?>
				<tr>
					<td width="25%" class="td-left"><?php echo $value['name']; ?></td>
					<td class="td-right"><textarea name="<?php echo $value['id']; ?>"
							style="width: 240px; height: 200px;"
							type="<?php echo $value['type']; ?>" cols="" rows="">
							<?php if ( get_option( $value['id'] ) != "") { echo get_option( $value['id'] ); } else { echo $value['std']; } ?>
						</textarea> <small><?php echo $value['desc']; ?> </small>
					</td>
				</tr>
				<?php break;
			case 'select':
				?>
				<tr>
					<td width="25%" class="td-left"><?php echo $value['name']; ?></td>
					<td class="td-right"><select class="selectbox"
						name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
						<?php foreach ($value['options'] as $option) { ?>
							<option
							<?php if ( get_option( $value['id'] ) == $option) {
								echo ' selected="selected"';
							}elseif ($option == $value['std']) {
								echo ' selected="selected"';
							}?>>
							<?php echo $option; ?>
							</option>
							<?php } ?>
					</select> <small><?php echo $value['desc']; ?> </small>
					</td>
				</tr>
				<?php break;
			case "checkbox":
				?>
				<tr>
					<td width="25%" class="td-left"><?php echo $value['name']; ?></td>
					<td class="td-right"><?php if(get_option($value['id'])){
						$checked = "checked=\"checked\"";
					}else{
						$checked = "";
					}
					?> <input type="checkbox" name="<?php echo $value['id']; ?>"
						id="<?php echo $value['id']; ?>" value="true"
						<?php echo $checked; ?> /> <small><?php echo $value['desc']; ?> </small>
					</td>
				</tr>
				<?php break;
			case 'categories':
				?>
				<tr>
					<td width="25%" class="td-left"><?php echo $value['name']; ?></td>
					<td class="td-right"><?php wp_dropdown_categories(
					array(
		                  'orderby'     =>  'name',
		                  'hide_empty'  =>  0,
		                  'selected'    =>  get_option($value['id']),
		                  'name'        =>  $value['id'],
		                  'class'       =>  'selectbox',
						  'hierarchical'=> 1));
					?> <small><?php echo $value['desc']; ?> </small>
					</td>
				</tr>
				<?php break;
			case 'pages':
				?>
				<tr>
					<td width="25%" class="td-left"><?php echo $value['name']; ?></td>
					<td class="td-right">
				<?php wp_dropdown_pages(
						array(
			                  'selected'    =>  get_option($value['id']),
			                  'name'        =>  $value['id'],
						)
					);
				?> <small><?php echo $value['desc']; ?> </small>
					</td>
				</tr>
				<?php break;
			}//end switch
		}//end foreach
		?>
			<p class="submit" style="padding: 0.5em 0;text-align:center;">
				<input name="save" type="submit" value="Lưu cấu hình" />
				<input type="hidden" name="action" value="save" />
			</p>
		</form>
		<form method="post" action="">
			<p class="submit" style="padding: 0.5em 0;text-align:center;">
				<input name="reset" type="submit" value="Khôi phục cài đặt gốc" />
				<input type="hidden" name="action" value="reset" />
			</p>
		</form>
	</div><!-- .wrap -->
	<?php }//end function vnwp_theme_admin
	add_action('admin_menu', 'theme_add_admin'); ?>